#include<stdio.h>
#include<sys/mman.h>
#include<sys/types.h>
#include<fcntl.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
#include<sys/ioctl.h>
#include<linux/fb.h>

#define RED 0x00FF0000//红色
#define PINK 0x00FF00FF//粉色
#define BLUE 0x000000FF//蓝色
#define YELLOW 0x00FFFF00//黄色

int main(int argc, char const *argv[])
{
    int lcd = open("/dev/fb0", O_RDWR);
    if (lcd == -1)
    {
        perror("open");
        exit(-1);
    }

    struct fb_var_screeninfo vinfo;
    struct fb_fix_screeninfo finfo;
    ioctl(lcd, FBIOGET_VSCREENINFO, &vinfo);

    int lcd_w = vinfo.xres;
    int lcd_h = vinfo.yres;
    int bpp   = vinfo.bits_per_pixel;
    int screen_size = lcd_w * lcd_h * bpp/8;

    //映射
    char *addr = mmap(NULL, screen_size * 2, PROT_READ|PROT_WRITE, MAP_SHARED, lcd, 0);
    if(addr == MAP_FAILED){
        perror("mmap");
        return -1;
    }

    vinfo.xres = 0;
    vinfo.yres = 0;
    ioctl(lcd, FBIOPAN_DISPLAY, &vinfo);

    int colors[] = {0x00FF0000, 0x00FF00FF, 0x000000FF, 0x00FFFF00};

    for(int k = 0;;k++){
        //当前要绘制的偏移量
        long offset = screen_size * ((k+1)%2);

        //铺满整个屏幕
        for(int i = 0; i < lcd_w * lcd_h; i++)
            memcpy(addr+4*i + offset, &colors[k%4], 4);

        vinfo.xoffset = 0;
        vinfo.yoffset = 480*((k+1)%2);
        ioctl(lcd, FBIOPAN_DISPLAY, &vinfo);

        sleep(1);
    }
    return 0;
}
